class AddRolesAndRights < ActiveRecord::Migration
  
  def self.create_right(name, controller, action)
    Right.create :name => name, :controller => controller, :action => action
  end
  
  def self.create_role(name)
    Role.create :name => name
    
  end
  def self.up
    rights = {
      :common => {
        :cancel => create_right('Peruuta', nil, 'cancel')
      },
      :customers => {
        :index =>  create_right('Etusivu', 'customers', 'index'),
        :list => create_right('Listaa', 'customers', 'list'),
        :show => create_right('Näytä', 'customers', 'show'),
        :edit => create_right('Muokkaa', 'customers', 'edit'),
        :update => create_right('Tallenna muutokset', 'customers', 'update'),
        :delete => create_right('Poista', 'customers', 'destroy'),
        :new => create_right('Uusi', 'customers', 'new'),
        :create => create_right('Tallenna', 'customers', 'create'),
        :delete_selected => create_right('Posta valitut', 'customers', 'destroy_selected'),
        
      },
      :addresses => {
        :list => create_right('Listaa', 'addresses', 'list'),
        :show => create_right('Näytä', 'addresses', 'show'),
        :new => create_right('Uusi', 'addresses', 'new'),
        :create => create_right('Tallennna', 'addresses', 'create'),
        :edit => create_right('Muokkaa', 'addresses', 'edit'),
        :update => create_right('Tallenna muutokset', 'addresses', 'update'),
        :delete => create_right('Poista', 'addresses', 'destroy'),
        :delete_selected => create_right('Poista valitut', 'addresses', 'destroy_selected')
        
      },
      :conversations =>  {
        :list => create_right('Listaa', 'conversations', 'list'),
        :show => create_right('Näytä', 'conversations', 'show'),
        :new => create_right('Uusi', 'conversations', 'new'),
        :create => create_right('Tallennna', 'conversations', 'create'),
        :edit => create_right('Muokkaa', 'conversations', 'edit'),
        :update => create_right('Tallenna muutokset', 'conversations', 'update'),
        :delete => create_right('Poista', 'conversations', 'destroy'),
        :delete_selected => create_right('Poista valitut', 'conversations', 'destroy_selected')
      },
      :contacts => {
        :list => create_right('Listaa', 'contacts', 'list'),
        :show => create_right('Näytä', 'contacts', 'show'),
        :new => create_right('Uusi', 'contacts', 'new'),
        :create => create_right('Tallennna', 'contacts', 'create'),
        :edit => create_right('Muokkaa', 'contacts', 'edit'),
        :update => create_right('Tallenna muutokset', 'contacts', 'update'),
        :delete => create_right('Poista', 'contacts', 'destroy'),
        :delete_selected => create_right('Poista valitut', 'contacts', 'destroy_selected')
      },
      :attachments => {
        :list => create_right('Listaa', 'attachments', 'list'),
        :show => create_right('Näytä', 'attachments', 'show'),
        :new => create_right('Uusi', 'attachments', 'new'),
        :create => create_right('Tallennna', 'attachments', 'create'),
        :edit => create_right('Muokkaa', 'attachments', 'edit'),
        :update => create_right('Tallenna muutokset', 'attachments', 'update'),
        :delete => create_right('Poista', 'attachments', 'destroy'),
        :delete_selected => create_right('Poista valitut', 'attachments', 'destroy_selected')
      },
      :quotes => {
        :list => create_right('Listaa', 'quotes', 'list'),
        :show => create_right('Näytä', 'quotes', 'show'),
        :new => create_right('Uusi', 'quotes', 'new'),
        :create => create_right('Tallennna', 'quotes', 'create'),
        :edit => create_right('Muokkaa', 'quotes', 'edit'),
        :update => create_right('Tallenna muutokset', 'quotes', 'update'),
        :delete => create_right('Poista', 'quotes', 'destroy'),
        :delete_selected => create_right('Poista valitut', 'quotes', 'destroy_selected')
      },
      :users => {
        :list => create_right('Listaa', 'users', 'list'),
        :new => create_right('Uusi', 'users', 'new'),
        :create => create_right('Tallenna', 'users', 'create'),
        :edit => create_right('Muokkaa', 'users', 'edit'),
        :update => create_right('Tallenna muutokset', 'users', 'update')
      }
      
      
      
    }
    roles = {
      :viewer => create_role('Peruskäyttäjä'),
      :power_user => create_role('Tehokäyttäjä'),
      :admin => create_role('Järjestelmänvalvoja'),
      :seller => create_role('Myyjä')
    }
    user = User.find_by_login('testi')
    user.roles << roles[:viewer]
    user.roles << roles[:admin]
    user.roles << roles[:power_user]
    user.roles << roles[:seller]
    user.save
    roles[:viewer].rights << rights[:common][:cancel]
    roles[:viewer].rights << rights[:customers][:list]
    roles[:viewer].rights << rights[:customers][:show]
    roles[:viewer].rights << rights[:customers][:index]
    roles[:viewer].rights << rights[:addresses][:list]
    roles[:viewer].rights << rights[:addresses][:show]
    roles[:viewer].rights << rights[:conversations][:list]
    roles[:viewer].rights << rights[:conversations][:show]
    roles[:viewer].rights << rights[:contacts][:list]
    roles[:viewer].rights << rights[:contacts][:show]
    roles[:viewer].rights << rights[:attachments][:list]
    roles[:viewer].rights << rights[:attachments][:show]
    roles[:viewer].rights << rights[:quotes][:list]
    roles[:viewer].rights << rights[:quotes][:show]
    roles[:viewer].save
    roles[:power_user].rights << rights[:customers][:edit]
    roles[:power_user].rights << rights[:customers][:update]
    roles[:power_user].rights << rights[:customers][:new]
    roles[:power_user].rights << rights[:customers][:create]
    roles[:power_user].rights << rights[:addresses][:edit]
    roles[:power_user].rights << rights[:addresses][:update]
    roles[:power_user].rights << rights[:addresses][:new]
    roles[:power_user].rights << rights[:addresses][:create]
    roles[:power_user].rights << rights[:conversations][:edit]
    roles[:power_user].rights << rights[:conversations][:update]
    roles[:power_user].rights << rights[:conversations][:new]
    roles[:power_user].rights << rights[:conversations][:create]
    roles[:power_user].rights << rights[:contacts][:edit]
    roles[:power_user].rights << rights[:contacts][:update]
    roles[:power_user].rights << rights[:contacts][:new]
    roles[:power_user].rights << rights[:contacts][:create]
    roles[:power_user].rights << rights[:attachments][:edit]
    roles[:power_user].rights << rights[:attachments][:update]
    roles[:power_user].rights << rights[:attachments][:new]
    roles[:power_user].rights << rights[:attachments][:create]
    roles[:power_user].save
    roles[:seller].rights << rights[:quotes][:edit]
    roles[:seller].rights << rights[:quotes][:update]
    roles[:seller].rights << rights[:quotes][:new]
    roles[:seller].rights << rights[:quotes][:create]
    roles[:admin].rights << rights[:customers][:delete_selected]
    roles[:admin].rights << rights[:customers][:delete]
    roles[:admin].rights << rights[:addresses][:delete_selected]
    roles[:admin].rights << rights[:addresses][:delete]
    roles[:admin].rights << rights[:conversations][:delete_selected]
    roles[:admin].rights << rights[:conversations][:delete]
    roles[:admin].rights << rights[:contacts][:delete_selected]
    roles[:admin].rights << rights[:contacts][:delete]
    roles[:admin].rights << rights[:attachments][:delete_selected]
    roles[:admin].rights << rights[:attachments][:delete]
    roles[:admin].rights << rights[:quotes][:delete_selected]
    roles[:admin].rights << rights[:quotes][:delete]
    roles[:admin].rights << rights[:users][:list]
    roles[:admin].rights << rights[:users][:new]
    roles[:admin].rights << rights[:users][:create]
    roles[:admin].rights << rights[:users][:edit]
    roles[:admin].rights << rights[:users][:update]
    roles[:admin].save
  end
  
  def self.down
    Right.find(:all).each do |right|
      right.destroy
    end
    
    Role.find(:all).each do |role|
      role.destroy
    end
  end
end
